import {
    ADD_COUNTER,
    ADD_TO_CART
} from './mutation-types'
export default {
    addCart(context, payloay) {
        //payload新添加的商品
        // let oldproduct = null
        // for(let item of state.cartList){
        //     if(item.iid === payload.iid){
        //         oldproduct = item

        //     }
        // }
        //1.查找以前的数组中是否有过这个商品
       return new Promise ((resolve,reject) => {
           let oldproduct = context.state.cartList.find(item => item.iid === payloay.iid)
           //2.判断old
           if (oldproduct) { //数量+1
               // oldproduct.count += 1
               context.commit(ADD_COUNTER, oldproduct)
               resolve('当前的数量+1')

           } else { //添加新的商品
               payloay.count = 1
               // context.state.cartList.push(payloay)
               context.commit(ADD_TO_CART, payloay)
               resolve('添加成功')

           }

       })

    }

}